<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="div-icon">
          <el-avatar icon="el-icon-user-solid" :size="50"> </el-avatar>
          <!--                    <i class="el-icon-user-solid" :size="80"></i>-->
        </div>
        <!--<span>登录</span>-->
      </div>
      <el-form
        ref="loginForm"
        :model="form"
        :rules="rules"
        label-position="left"
        label-width="60px"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            placeholder="请输入账号"
            v-model="form.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="isRem">
          <el-checkbox label="记住密码" v-model="form.isRem"></el-checkbox>
        </el-form-item>
        <el-button-group>
          <el-button type="primary" @click="login" :loading="isLoading"
            >登录</el-button
          >
          <el-button type="primary" @click="reset">重置</el-button>
        </el-button-group>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// import http from '../plugins/http.js'
import { api_login } from '../api/admin.js'

export default {
  data() {
    return {
      isLoading: false,
      form: {
        username: '',
        password: '',
        isRem: false
      },
      rules: {
        username: [
          { required: true, message: '请输入账号！', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码！', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.isLoading = true
      // 1.验证表单
      this.$refs.loginForm.validate(async valid => {
        if (!valid) {
          console.log('验证失败!')
          return false
        }
        // 2.提交表单
        const {data: result} = await api_login(
          this.form.username,
          this.form.password
        )
        // console.log(result)
        this.isLoading = false
        if (result.code !== 200) {
          this.$message.error(result.message)
          return true
        }
        const data = result.data
        const token = data.tokenHead + data.token
        // 3.保存token到sessionStorage
        window.sessionStorage.setItem('token', token) //会话结束后，自动消除
        // 4.保存用户信息到vuex中的store
        // 5.记住密码功能，登录用户密码保存到cookies
        if (this.form.isRem) {
          this.$cookies.set('username', this.form.username, '7D') //这里设置七天过期，默认的是一天过期
          this.$cookies.set('password', this.form.password, '7D')
        } else {
          this.$cookies.remove('username')
          this.$cookies.remove('password')
        }
        // 6.登录页面刷新或重新加载的时候从cookies中获取
        // 7.跳转到主页面,编程式路由跳转
        this.$router.replace('/home')
      })
    },

    reset() {
      this.$refs['loginForm'].resetFields()
      this.form.username = ''
      this.form.password = ''
      this.form.isRem = false
    }
  },
  created() {
    //获取cookies中的值
    this.form.username = this.$cookies.get('username')
    this.form.password = this.$cookies.get('password')
    if (this.form.username) {
      this.form.isRem = true
    }
  }
}
</script>

<style scoped>
/*  scoped:表示该组件中的样式只是在当前组件中有效 */
.div-icon {
  transform: translateX(45%);
  /*transform: translateY(50%);*/
  /*margin-top: 50px;*/
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
  margin: auto;
  transform: translateY(50%);
}

.el-button-group {
  width: 100%;
}

.el-button-group > .el-button {
  width: 50%;
}
</style>
